<!--
  * 数据范围范围编辑 表单 
  *
-->
<template>
  <a-modal :title="form.id ? '编辑数据权限' : '添数据权限'" :width="600" :visible="modalVisible" @cancel="onClose" :footer="null">
    <a-form ref="formRef" :model="form" :rules="rules" :labelCol="{ span: 4 }">
      <a-form-item label="用户" name="userId">
        <DictSelect :service-id="SERVICE_ID_ENUM.USER.value"  v-model:value="form.userId"  />
      </a-form-item>
      <a-row :gutter="24">
        <a-col :span="2"></a-col>
        <a-col :span="11">
        <a-form-item label="用户范围" name="userFlag" :labelCol="{span:8}">
          <a-switch v-model:checked="form.userFlag" checked-children="启用" checkedValue="Y"  un-checked-children="禁用" unCheckedValue="N" />
        </a-form-item>
        </a-col>
        <a-col :span="11">
        <a-form-item label="岗位关联机构范围" name="quarterOrgFlag" :labelCol="{span:10}">
          <a-switch v-model:checked="form.quarterOrgFlag" checked-children="启用" checkedValue="Y"  un-checked-children="禁用" unCheckedValue="N" />
        </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :span="2"></a-col>
        <a-col :span="10">
      <a-form-item label="机构范围" name="orgFlag" :labelCol="{span:8}">
        <a-switch v-model:checked="form.orgFlag" checked-children="启用" checkedValue="Y"  un-checked-children="禁用" unCheckedValue="N" />
      </a-form-item>
    </a-col>
        <a-col :span="12">
      <a-form-item label="岗位范围" name="quarterFlag" :labelCol="{span:8}">
        <a-switch v-model:checked="form.quarterFlag" checked-children="启用" checkedValue="Y"  un-checked-children="禁用" unCheckedValue="N" />
      </a-form-item>
    </a-col>
      </a-row>
      <a-form-item label="排序" name="showOrder">
          <a-input-number v-model:value="form.showOrder" :min="0" placeholder="请输入排序号" style="width: 80%" />
          <span style="color: #ababab">值越小越靠前</span>
        </a-form-item>
      <a-form-item label="备注" name="remark">
        <a-input style="width: 100%" placeholder="请输入备注" v-model:value="form.remark" />
      </a-form-item>
      
    </a-form>

    <div class="footer">
      <a-button style="margin-right: 8px" @click="onClose">取消</a-button>
      <a-button type="primary" @click="submitForm">提交</a-button>
    </div>
  </a-modal>
</template>

<script setup lang="ts">
  import { message } from 'ant-design-vue';
  import { reactive, ref } from 'vue';
  import { dsApi } from '/@/api/system/auth/ds-api'
  import { smartSentry } from '/@/lib/smart-sentry';
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import { SERVICE_ID_ENUM,DICT_TYPE_ENUM } from '/@/constants/system/cache-service-id-const';
  import DictSelect from '/@/components/cache/dict-select.vue';
import { fromPairs } from 'lodash';
  // ----------------------- 以下是字段定义 emits props ---------------------
  let emits = defineEmits(['refresh']);

  defineExpose({
    showModal,
  });

  // ----------------------- modal 显示与隐藏 ---------------------
  const modalVisible = ref(false);

  function showModal(ds) {
    Object.assign(form, formDefault);
    if (ds) {
      Object.assign(form, ds);
    }
    modalVisible.value = true;
  }

  function onClose() {
    Object.assign(form, formDefault);
    modalVisible.value = false;
  }

  // ----------------------- 表单 ---------------------

  const formRef = ref();

  const formDefault = {
    id: undefined,
    userId: undefined,
    userFlag: "Y",
    orgFlag: "N",
    quarterFlag: "N",
    quarterOrgFlag:"N",
    state:"Y",
    remark: undefined,
    showOrder:undefined,
  };

  let form = reactive({ ...formDefault });

  // 表单规则
  const rules = {
    userId: [{ required: true, message: '请输入用户名称' }],
  };

  // 提交表单
  async function submitForm() {
    formRef.value
      .validate()
      .then(async () => {
        SmartLoading.show();
        try {
          if(form.quarterOrgFlag==='Y' && (form.orgFlag==='Y' || form.quarterFlag==='Y'))
            message.error("岗位关联组织模式不能与组织或岗位模式并存！");
          else{
            let res=await dsApi.updateDatascope(form);
            if(res.success){
              message.success(res.message);
              emits('refresh');
              onClose();
            }
            else
              message.error(res.message);
          }
        } catch (e) {
          smartSentry.captureError(e);
        } finally {
          SmartLoading.hide();
        }
      })
      .catch((error) => {
        message.error('参数验证错误，请仔细填写表单数据!');
      });
  }
</script>

<style scoped lang="less">
  .footer {
    width: 100%;
    border-top: 1px solid #e9e9e9;
    padding: 10px 16px;
    background: #fff;
    text-align: right;
    z-index: 1;
  }
</style>
